{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Decimals: Performance Considerations"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Memory Footprint"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Decimals take up a lot more memory than floats."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import sys\n",
    "from decimal import Decimal"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "a = 3.1415\n",
    "b = Decimal('3.1415')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "24"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sys.getsizeof(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "24 bytes are used to store the float 3.1415"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "104"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sys.getsizeof(b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "104 bytes are used to store the Decimal 3.1415"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Computational Performance"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Decimal arithmetic is also much slower than float arithmetic (on a CPU, and even more so if using a GPU)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can do some rough timings to illustrate this."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "First we look at the performance difference creating floats vs decimals:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import time\n",
    "from decimal import Decimal\n",
    "\n",
    "def run_float(n=1):\n",
    "    for i in range(n):\n",
    "        a = 3.1415\n",
    "        \n",
    "def run_decimal(n=1):\n",
    "    for i in range(n):\n",
    "        a = Decimal('3.1415')\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Timing float and Decimal operations:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "n = 10000000"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "float:  0.21406484986433047\n",
      "decimal:  2.1353148079910156\n"
     ]
    }
   ],
   "source": [
    "start = time.perf_counter()\n",
    "run_float(n)\n",
    "end = time.perf_counter()\n",
    "print('float: ', end-start)\n",
    "\n",
    "start = time.perf_counter()\n",
    "run_decimal(n)\n",
    "end = time.perf_counter()\n",
    "print('decimal: ', end-start)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We make a slight variant here to see how addition compares between the two types:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "float:  0.1875864573764936\n",
      "decimal:  0.3911394302055555\n"
     ]
    }
   ],
   "source": [
    "def run_float(n=1):\n",
    "    a = 3.1415\n",
    "    for i in range(n):\n",
    "        a + a\n",
    "        \n",
    "def run_decimal(n=1):\n",
    "    a = Decimal('3.1415')\n",
    "    for i in range(n):\n",
    "        a + a\n",
    "        \n",
    "start = time.perf_counter()\n",
    "run_float(n)\n",
    "end = time.perf_counter()\n",
    "print('float: ', end-start)\n",
    "\n",
    "start = time.perf_counter()\n",
    "run_decimal(n)\n",
    "end = time.perf_counter()\n",
    "print('decimal: ', end-start)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "How about square roots:\n",
    "\n",
    "(We drop the n count a bit)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "float:  0.673833850211659\n",
      "decimal:  14.73112183459776\n"
     ]
    }
   ],
   "source": [
    "n = 5000000\n",
    "\n",
    "import math\n",
    "\n",
    "def run_float(n=1):\n",
    "    a = 3.1415\n",
    "    for i in range(n):\n",
    "        math.sqrt(a)\n",
    "        \n",
    "def run_decimal(n=1):\n",
    "    a = Decimal('3.1415')\n",
    "    for i in range(n):\n",
    "        a.sqrt()\n",
    "        \n",
    "start = time.perf_counter()\n",
    "run_float(n)\n",
    "end = time.perf_counter()\n",
    "print('float: ', end-start)\n",
    "\n",
    "start = time.perf_counter()\n",
    "run_decimal(n)\n",
    "end = time.perf_counter()\n",
    "print('decimal: ', end-start)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
